package com.localjoycommunity.cpdj.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.localjoycommunity.cpdj.project.system.domain.SysBeauty;
import com.localjoycommunity.cpdj.project.system.domain.User;
import com.localjoycommunity.cpdj.project.system.domain.UserBeauty;
import com.localjoycommunity.cpdj.project.system.domain.UserCompany;
import com.localjoycommunity.cpdj.project.system.mapper.UserBeautyMapper;
import com.localjoycommunity.cpdj.project.system.mapper.UserMapper;
import com.localjoycommunity.cpdj.project.system.service.ISysBeautyService;
import com.localjoycommunity.cpdj.project.system.service.IUserBeautyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lida
 * @Date: 2020/11/14 0014 上午 10:50
 * @Description:
 */
@Service
public class UserBeautyServiceImpl extends ServiceImpl<UserBeautyMapper, UserBeauty> implements IUserBeautyService {

    @Autowired
    private ISysBeautyService sysBeautyService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户点赞风采
     *
     * @param userBeauty
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int beautyDig(UserBeauty userBeauty) {
        // 1. 先查询
        UserBeauty one = baseMapper.selectOne(new QueryWrapper<UserBeauty>().lambda().eq(UserBeauty::getUserId, userBeauty.getUserId())
                .eq(UserBeauty::getBeautyId, userBeauty.getBeautyId()));
        if (one == null) {
            userBeauty.setIsDig(1);
            sysBeautyService.updateDigNum(userBeauty.getBeautyId(), 1);
            return baseMapper.insert(userBeauty);
        }
        User user = userMapper.selectUserById(userBeauty.getUserId());
        Long digNum = user.getDigNum() == null ? 0 : user.getDigNum();
        one.setIsDig(one.getIsDig() ^ 1);
        if (one.getIsDig() == 1) {
            sysBeautyService.updateDigNum(userBeauty.getBeautyId(), 1);
            user.setDigNum(digNum + 1);
        } else {
            sysBeautyService.updateDigNum(userBeauty.getBeautyId(), -1);
            digNum = digNum - 1 <= 0 ? 0 : digNum - 1;
            user.setDigNum(digNum);
        }
        userMapper.updateUser(user);
        return baseMapper.updateById(one);
    }

    @Override
    public List<SysBeauty> selectUserBeauty(Integer userId) {
        List<UserBeauty> userBeauties = baseMapper.selectList(new QueryWrapper<UserBeauty>().lambda().eq(UserBeauty::getUserId, userId).eq(UserBeauty::getIsDig, 1));
        if (userBeauties == null || userBeauties.size() == 0) {
            return null;
        }
        List<SysBeauty> list = new ArrayList<>();
        for (UserBeauty userBeauty : userBeauties) {
            SysBeauty sysBeauty = sysBeautyService.selectAppSysBeautyById(userBeauty.getBeautyId());
            list.add(sysBeauty);
        }
        return list;
    }

    @Override
    public Integer isDig(int userId, Long beautyId) {
        return baseMapper.isDig(userId, beautyId).orElse(0);
    }
}
